package spring.tutorial.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import spring.tutorial.domain.Moim;

@Repository
@Transactional
public class MoimDaoImpl implements MoimDao {

	@Autowired
	private SessionFactory factory;

	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<Moim> getAll() {
		return factory.getCurrentSession().createQuery("from Moim").list();
	}

	@Transactional(readOnly = true)
	public Moim get(long id) {
		return (Moim) factory.getCurrentSession().get(Moim.class, id);
	}

	public void save(Moim moim) {
		factory.getCurrentSession().save(moim);
	}

	public void saveOrUpdate(Moim moim) {
		factory.getCurrentSession().saveOrUpdate(moim);
	}

	public void delete(long moimId) {
		Moim moim = get(moimId);
		factory.getCurrentSession().delete(moim);
	}
}
